;define(function (require) {
	'use strict';
	var $=require('jquery');
	var logs=require('plugin/logs');
	var echart3=require('plugin/echarts.min');
		require('plugin/superslide');
		require('layer');
	var config = require('config');
	var $Fun = require('functions');
	
	var reqConfig = {};
	if(config.debugModel){
		reqConfig = config.debugAjaxRequestUrls;//测试数据
	}else{
		reqConfig = config.urlConfig;//接口配置
	}
	
	var app = {};
	app.init = function(){
		//十六进制转rgba
		String.prototype.color2Rgba=function(){var sColor=this.toLowerCase(),reg=/^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;if(sColor&&reg.test(sColor)){if(sColor.length===4){var sColorNew="#";for(var i=1;i<4;i+=1){sColorNew+=sColor.slice(i,i+1).concat(sColor.slice(i,i+1))}sColor=sColorNew}var sColorChange=[];for(var i=1;i<7;i+=2){sColorChange.push(parseInt("0x"+sColor.slice(i,i+2)))}return"RGBA("+sColorChange.join(",")+","}else{return sColor}};
		
		logs.log('loaded.');

		var _nav=require('plugin/nav');
		var nav=new _nav('#navbox','#navpoint');
		nav.run();
		
		$('#navbox li.menu').click(function(e){
			var el = window.event.srcElement || e.target;
			if(el.nodeName=="LI"){
				if($(this).find('a').length>0 && $(this).find('a').attr('href').indexOf('javascript')<0 ){
					location.href = $(this).find('a').attr('href'); 
				}
			}
		})
		
		//缓存数据
		//_cache['url1-2015-09']
		//经济监测>经济监测
		_cache['url1-time'] = '2015-09';
		$Fun.setCacheData( reqConfig.url1+'?year=2015&month=09', 'url1-'+_cache['url1-time'] );
		
		//图表生成
		$Fun.drawChart1( '.tv-chart-a', _cache['url1-'+_cache['url1-time']].g1 );
		$Fun.drawChart1( '.tv-chart-b', _cache['url1-'+_cache['url1-time']].g2 );
		$Fun.drawChart1( '.tv-chart-c', _cache['url1-'+_cache['url1-time']].g3 );
		$Fun.drawChart1( '.tv-chart-d', _cache['url1-'+_cache['url1-time']].g4 );
		$Fun.drawChart1( '.tv-chart-e', _cache['url1-'+_cache['url1-time']].g5 );
		
		var myChart;
		/*--------ABCDEFGHIJK------------*/
		/*$Fun.drawChartA( '#chart3-1', _cache['urlA-'+_cache['urlA-time']], function(rtn){
			var option = rtn;
			echart3.init($('#chart3-1')[0]).setOption(option, true);
		} );*/
		//省去重复的步骤
		var plFun = function plFun(key, dom){
			$Fun.setCacheData( reqConfig['url'+key], 'url'+key+'-'+_cache['url'+key+'-time'] );
			
			var d = _cache['url'+key+'-'+_cache['url'+key+'-time']];
			if(d){
				$Fun['drawChart'+key]( dom, d, function(rtn){
					var option = rtn;
					echart3.init($(dom)[0]).setOption(option, true);
				});
			}else{
				echart3.getInstanceByDom($(dom)[0]).clear();
			}
		}
		
		function initLevel2(){
			setTimeout(function(){
				_cache['urlAa-time'] = '2014-10';
				plFun('Aa', '#chart3-4');
			},500)
			setTimeout(function(){
				_cache['urlBb-time'] = '2014-10';
				plFun('Bb', '#chart3-5');
			},1000)
		}
		
		function initLevel1(){
			setTimeout(function(){
				_cache['urlA-time'] = '2014-10';
				plFun('A', '#chart3-1');
			},500)
			setTimeout(function(){
				_cache['urlB-time'] = '2015-09';
				plFun('B', '#chart3-2');
			},1000)
			setTimeout(function(){
				_cache['urlC-time'] = '2015-09';
				plFun('C', '#chart3-3');
			},1500)
			
			//bind down show later
			$(".JQ_downShow").off("click").on("click",function(){
				$(".JQ_downShowLater").removeClass("fadeInDown");
				setTimeout(function(){
					$(".JQ_downShowLater").removeClass("hidden fadeInDown").addClass("fadeInDown");
					initLevel2();
				},1);
			})
		}
		
		$(".JQ_downShowTriger li").off("click").on("click",function(){
			$(".JQ_downShow").removeClass("fadeInDown");
			setTimeout(function(){
				$(".JQ_downShow").removeClass("hidden fadeInDown").addClass("fadeInDown");
				initLevel1();
			},1);
		})
		
		_cache['urlD-time'] = '2015-09';
		plFun('D', '#chart4-1-1');
		_cache['urlE-time'] = '2015-09';
		plFun('E', '#chart4-1-2');
		_cache['urlF-time'] = '2015-09';
		plFun('F', '#chart4-1-3');
		_cache['urlG-time'] = '2015-02';
		plFun('G', '#chart4-1-4');
		
		_cache['urlH-time'] = '2015-09';
		plFun('H', '#chart4-2-1');
		_cache['urlI-time'] = '2015-01';
		plFun('I', '#chart4-2-2');
		_cache['urlJ-time'] = '2015-09';
		plFun('J', '#chart4-2-3');
		_cache['urlK-time'] = '2015-01';
		plFun('K', '#chart4-2-4');
		
		
		//layer trigger event
		$(".JQ_layerTriger").off("click").on("click",function(){
			var $this = $(this);
			var ob = $this.find('.chartWrap'),
					obmubiaoId = ob.attr('id'),
					obechartIsc = echart3.getInstanceByDom($('[id='+obmubiaoId+']')[0]),
					obmubiaoOption = obechartIsc.getOption();
			if(obmubiaoOption.yAxis && obmubiaoOption.yAxis.length>1 && obmubiaoOption.title[0].text=="工业用电量"){
				//pass
			}else{
				return false;
			}
			
			layer.open({
				type:1
				,title:false
				,content:$this.html()
				,area:['800px','400px']
				,skin:'bg-white-boxshadow'
				,fix:true
				,success:function(layero, index){
					//animated
					layero.addClass('animated bounceIn');
					
					//rechart
					var o = layero.find('.chartWrap'),
					mubiaoId = o.attr('id');
					o.remove();
					var echartIsc = echart3.getInstanceByDom($('[id='+mubiaoId+']')[0]);
					var mubiaoOption = echartIsc.getOption();
					//new html
					var html = '<div class="tempChartWrap fl"></div><div class="tempChartWrap fr"></div>';
					layero.find('.tv-body').append(html);
					var mapChart = echart3.init($('.tempChartWrap')[0]);
					console.log(mubiaoOption);
					if(mubiaoOption.yAxis && mubiaoOption.yAxis.length>1 && mubiaoOption.title[0].text=="工业用电量"){
						var tmp = [mubiaoOption.yAxis[0],mubiaoOption.yAxis[1]];
						var tmp2 = [mubiaoOption.series[0],mubiaoOption.series[1]];
						//分左右
						mubiaoOption.yAxis = tmp[0];
						mubiaoOption.series = [tmp2[1]];
						mubiaoOption.series[0].yAxisIndex = 0;
						mubiaoOption.tooltip[0].formatter = "{b0}<br />总量: {c0}";
						mapChart.setOption(mubiaoOption);
						
						var mapChart2 = echart3.init($('.tempChartWrap')[1]);
						mubiaoOption.yAxis = tmp[1];
						mubiaoOption.series = [tmp2[0]];
						mubiaoOption.series[0].yAxisIndex = 0;
						mubiaoOption.tooltip[0].formatter = "{b0}<br />增幅: {c0}%";
						mapChart2.setOption(mubiaoOption);
//						tmp2.yAxis = mubiaoOption.yAxis[1];
//						tmp2.series = [mubiaoOption.series[0]];
//						var mapChart2 = echart3.init($('.tempChartWrap')[1]);
//						console.log(tmp2);
//						mapChart2.setOption(tmp2);
					}else{
						mapChart.setOption(mubiaoOption);
					}
					
					//bind close
					layero.find(".fa-remove").off("click").on("click",function(){
						layero.addClass('animated bounceOut');
						setTimeout(function(){
							$('.layui-layer-shade').remove();
							$('.layui-layer').remove();
						},750);
					})
				}
			});
		})
	};
	return app;
});
